<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\Message;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Traits\SendTemplateMsg;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;


class CrontabController extends Controller
{
    use SendTemplateMsg;
    public function setOrderNotice()
    {
        $message = Message::with('order')->where('status',0)->first();
        Log::info("timeL:".date("Y-m-d H:i:s",time()));
        if($message) {
            $order = Order::find($message->order_id);
            $time = $order->time_end;
            $money = $order->cash_money . '元';
            $address = Address::find($order->address_id);
            $addressStr = $address->district . $address->detail;
            $user = $address->consignee;
            if($message->type =='new') {
                $type = 'new';
                $orderProduct = OrderProduct::with('product')->where('order_id', $message->order_id)->first();
                $str = $orderProduct->product['title'] . "等商品";
                $data = ['name' => $str, 'money' => $money, 'time' => $time, 'user' => $user, 'address' => $addressStr];
            }else{
                $type = 'refund';
                $orderProduct = OrderProduct::with('product')->where('order_id', $message->order_id)->first();
                $str = $orderProduct->product['title'] . "等商品";
                $data = ['order_no' => $order->no, 'name' => $str, 'money' => $money, 'time' => $time, 'way' => '原路退回'];
            }
            $this->sendTemplate($type,$data);
            $message->status = 1;
            $message->send_to = 'oRo2F5MBb1D65nQm7UbZV7-izT8s';
            $message->save();
        }
    }
}
